package com.donfelix.looknews.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.donfelix.looknews.R;
import com.donfelix.looknews.common.LookNewsApplication;

/**
 * Created by Don on 2017/3/27.
 * Desc
 */

public class SPUtils {
    /**
     * 私有化构造方法，不允许被实例化
     */
    private SPUtils() {
        throw new UnsupportedOperationException("can't be instantiated");
    }

    private static SharedPreferences getSharedPreferences() {
        return PreferenceManager.getDefaultSharedPreferences(LookNewsApplication.getContext());
    }

    public static void putPrefString(String key, String value) {
        getSharedPreferences().edit().putString(key, value).commit();
    }

    public static String getPrefString(String key, String defValue) {
        return getSharedPreferences().getString(key, defValue);
    }

    public static boolean getPrefBoolean(String key, boolean defValue) {
        return getSharedPreferences().getBoolean(key, defValue);
    }

    public static void setPrefBoolean(String key, boolean value) {
        getSharedPreferences().edit().putBoolean(key, value).commit();
    }

    public static int getPrefInt(String key, int defValue) {
        return getSharedPreferences().getInt(key, defValue);
    }

    public static void setPrefInt(String key, int value) {
        getSharedPreferences().edit().putInt(key, value).commit();
    }

    public static float getPrefFloat(String key, float defValue) {
        return getSharedPreferences().getFloat(key, defValue);
    }

    public static void setPrefFloat(String key, float value) {
        getSharedPreferences().edit().putFloat(key, value).commit();
    }

    public static long getPrefLong(String key, long defValue) {
        return getSharedPreferences().getLong(key, defValue);
    }

    public static void setPrefLong(String key, long value) {
        getSharedPreferences().edit().putLong(key, value).commit();
    }

    public static boolean hasKey(String key) {
        return getSharedPreferences().contains(key);
    }

    public static void clearPreferences() {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.clear();
        editor.commit();
    }

    public static final String SHARED_PREFERENCE_NAME = "micro_reader";
    public static final String IMAGE_DESCRIPTION = "image_description";
    public static final String VIBRANT = "vibrant";
    public static final String MUTED = "muted";
    public static final String IMAGE_GET_TIME = "image_get_time";
    public static final String SAVED_CHANNEL = "saved_channel";

    public static boolean isRefreshOnlyWifi(Context context) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPreferences.getBoolean(context.getResources().getString(R.string.pre_refresh_data), false);
    }

    public static boolean isChangeThemeAuto(Context context) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPreferences.getBoolean(context.getResources().getString(R.string.pre_get_image), true);
    }

    public static boolean isImmersiveMode(Context context) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPreferences.getBoolean(context.getString(R.string.pre_status_bar), true);
    }

    public static boolean isChangeNavColor(Context context) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPreferences.getBoolean(context.getString(R.string.pre_nav_color), true);
    }

    public static boolean isUseLocalBrowser(Context context) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPreferences.getBoolean(context.getString(R.string.pre_use_local), false);
    }

    public static int getNevigationItem(Context context) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPreferences.getInt(context.getString(R.string.nevigation_item), -1);
    }

    public static void putNevigationItem(Context context, int t) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(context.getString(R.string.nevigation_item), t);
        editor.commit();
    }
}
